package com.kiln.juxingqs.ui.safe

import android.content.Intent
import android.text.TextUtils
import androidx.lifecycle.Observer
import client.global.commlib.base.FinishReciver
import client.global.commlib.toast.toast
import client.global.commlib.utils.SharedUtil
import com.kiln.juxingqs.BaseMiddleActivity
import com.kiln.juxingqs.R
import com.kiln.juxingqs.databinding.ActivityUpdatePhoneBinding
import com.kiln.juxingqs.dialog.MsgTitleDialog
import com.kiln.juxingqs.ui.LoginActivity
import com.kiln.juxingqs.ui.vm.WjmmViewModel
import java.util.*

/**
 * 修改手机号
 */
class UpdatePhoneActivity : BaseMiddleActivity<WjmmViewModel, ActivityUpdatePhoneBinding>() {

    var timerTask: TimerTask? = null
    var timer: Timer? = null
    override fun getLayout(): Int {
        return R.layout.activity_update_phone
    }

    override fun initView() {
        binding?.apply {
            event = this@UpdatePhoneActivity
            vm = viewModel

            var filed = intent.getStringExtra("filed")
            viewModel.account.value = filed
        }
    }

    override fun observerUI() {

        viewModel.codeRet.observe(this, Observer {

            it?.apply {

                if (isSuccess) {

                    toast("获取验证码成功")

                    timerTask?.let { it.cancel() }
                    timer?.let { it.cancel() }

                    viewModel.getCodeEnable.postValue(false)

                    timerTask = object : TimerTask() {
                        var count = 60
                        override fun run() {
                            count--;
                            viewModel.getCodeTxt.postValue("${count}秒后获取")
                            if (count <= 0) {
                                viewModel.getCodeEnable.postValue(true)
                                viewModel.getCodeTxt.postValue("获取验证码")
                                timerTask!!.cancel()

                            }
                        }
                    }
                    timer = Timer().apply { schedule(timerTask, 0, 1000) }

                } else {
//                    toast(it.message)
                    viewModel.getCodeEnable.value = true
                    viewModel.getCodeTxt.value = "获取验证码"
                }

            }
        })
        viewModel.okRet.observe(this, Observer {
            it?.apply {
                if (isSuccess) {
                    toast("修改成功，请重新登录")

                    SharedUtil.clear("token")
                    startActivity(Intent(this@UpdatePhoneActivity, LoginActivity::class.java))
                    sendBroadcast(Intent(FinishReciver.FINISH_ACTION))

//                    finish()
                } else {
                    viewModel.getCodeEnable.value = true
                    viewModel.getCodeTxt.value = "获取验证码"
                }

            }
        })
    }



    fun is_ok_update_phone() {
        if (TextUtils.isEmpty(viewModel.account.value) || TextUtils.isEmpty(viewModel.account_new.value) || TextUtils.isEmpty(viewModel.smscode.value)) {
            toast("所有不能为空")
            return
        }
        MsgTitleDialog(this).apply {
            okTxt = "确定"
            cancleTxt = "关闭"
            title = "确定修改为新的手机号？"
            ok = {
                viewModel.ok_update_phone()
            }
        }.show()
    }

    override fun onDestroy() {

        timerTask?.let { it.cancel() }
        timer?.let { it.cancel() }

        super.onDestroy()
    }


    override fun initViewModel(): WjmmViewModel = createViewModel()

    fun back() {
        finish()
    }
}